{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "FileNotFoundError",
     "evalue": "[Errno 2] No such file or directory: '2.xlsx'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mFileNotFoundError\u001b[0m                         Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[1], line 8\u001b[0m\n\u001b[1;32m      6\u001b[0m \u001b[38;5;66;03m# 加载Excel文件\u001b[39;00m\n\u001b[1;32m      7\u001b[0m file_path \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m2.xlsx\u001b[39m\u001b[38;5;124m'\u001b[39m  \u001b[38;5;66;03m# 请将此处替换为你的文件实际路径\u001b[39;00m\n\u001b[0;32m----> 8\u001b[0m df \u001b[38;5;241m=\u001b[39m \u001b[43mpd\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mread_excel\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfile_path\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m     10\u001b[0m \u001b[38;5;66;03m# 提取第三列\u001b[39;00m\n\u001b[1;32m     11\u001b[0m image_column \u001b[38;5;241m=\u001b[39m df\u001b[38;5;241m.\u001b[39miloc[:, \u001b[38;5;241m2\u001b[39m]\n",
      "File \u001b[0;32m~/anaconda3/envs/as2/lib/python3.8/site-packages/pandas/io/excel/_base.py:478\u001b[0m, in \u001b[0;36mread_excel\u001b[0;34m(io, sheet_name, header, names, index_col, usecols, dtype, engine, converters, true_values, false_values, skiprows, nrows, na_values, keep_default_na, na_filter, verbose, parse_dates, date_parser, date_format, thousands, decimal, comment, skipfooter, storage_options, dtype_backend)\u001b[0m\n\u001b[1;32m    476\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(io, ExcelFile):\n\u001b[1;32m    477\u001b[0m     should_close \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m\n\u001b[0;32m--> 478\u001b[0m     io \u001b[38;5;241m=\u001b[39m \u001b[43mExcelFile\u001b[49m\u001b[43m(\u001b[49m\u001b[43mio\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mstorage_options\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstorage_options\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mengine\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mengine\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    479\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m engine \u001b[38;5;129;01mand\u001b[39;00m engine \u001b[38;5;241m!=\u001b[39m io\u001b[38;5;241m.\u001b[39mengine:\n\u001b[1;32m    480\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[1;32m    481\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mEngine should not be specified when passing \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m    482\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124man ExcelFile - ExcelFile already has the engine set\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m    483\u001b[0m     )\n",
      "File \u001b[0;32m~/anaconda3/envs/as2/lib/python3.8/site-packages/pandas/io/excel/_base.py:1496\u001b[0m, in \u001b[0;36mExcelFile.__init__\u001b[0;34m(self, path_or_buffer, engine, storage_options)\u001b[0m\n\u001b[1;32m   1494\u001b[0m     ext \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mxls\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m   1495\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m-> 1496\u001b[0m     ext \u001b[38;5;241m=\u001b[39m \u001b[43minspect_excel_format\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m   1497\u001b[0m \u001b[43m        \u001b[49m\u001b[43mcontent_or_path\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mpath_or_buffer\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mstorage_options\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstorage_options\u001b[49m\n\u001b[1;32m   1498\u001b[0m \u001b[43m    \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m   1499\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m ext \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m   1500\u001b[0m         \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[1;32m   1501\u001b[0m             \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mExcel file format cannot be determined, you must specify \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m   1502\u001b[0m             \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124man engine manually.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m   1503\u001b[0m         )\n",
      "File \u001b[0;32m~/anaconda3/envs/as2/lib/python3.8/site-packages/pandas/io/excel/_base.py:1371\u001b[0m, in \u001b[0;36minspect_excel_format\u001b[0;34m(content_or_path, storage_options)\u001b[0m\n\u001b[1;32m   1368\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(content_or_path, \u001b[38;5;28mbytes\u001b[39m):\n\u001b[1;32m   1369\u001b[0m     content_or_path \u001b[38;5;241m=\u001b[39m BytesIO(content_or_path)\n\u001b[0;32m-> 1371\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m \u001b[43mget_handle\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m   1372\u001b[0m \u001b[43m    \u001b[49m\u001b[43mcontent_or_path\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mrb\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mstorage_options\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstorage_options\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mis_text\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mFalse\u001b[39;49;00m\n\u001b[1;32m   1373\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m \u001b[38;5;28;01mas\u001b[39;00m handle:\n\u001b[1;32m   1374\u001b[0m     stream \u001b[38;5;241m=\u001b[39m handle\u001b[38;5;241m.\u001b[39mhandle\n\u001b[1;32m   1375\u001b[0m     stream\u001b[38;5;241m.\u001b[39mseek(\u001b[38;5;241m0\u001b[39m)\n",
      "File \u001b[0;32m~/anaconda3/envs/as2/lib/python3.8/site-packages/pandas/io/common.py:868\u001b[0m, in \u001b[0;36mget_handle\u001b[0;34m(path_or_buf, mode, encoding, compression, memory_map, is_text, errors, storage_options)\u001b[0m\n\u001b[1;32m    859\u001b[0m         handle \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mopen\u001b[39m(\n\u001b[1;32m    860\u001b[0m             handle,\n\u001b[1;32m    861\u001b[0m             ioargs\u001b[38;5;241m.\u001b[39mmode,\n\u001b[0;32m   (...)\u001b[0m\n\u001b[1;32m    864\u001b[0m             newline\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m    865\u001b[0m         )\n\u001b[1;32m    866\u001b[0m     \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m    867\u001b[0m         \u001b[38;5;66;03m# Binary mode\u001b[39;00m\n\u001b[0;32m--> 868\u001b[0m         handle \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mopen\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mhandle\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mioargs\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmode\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    869\u001b[0m     handles\u001b[38;5;241m.\u001b[39mappend(handle)\n\u001b[1;32m    871\u001b[0m \u001b[38;5;66;03m# Convert BytesIO or file objects passed with an encoding\u001b[39;00m\n",
      "\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: '2.xlsx'"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import json\n",
    "import requests\n",
    "import pandas as pd\n",
    "\n",
    "# 加载Excel文件\n",
    "file_path = '2.xlsx'  # 请将此处替换为你的文件实际路径\n",
    "df = pd.read_excel(file_path)\n",
    "\n",
    "# 提取第三列\n",
    "image_column = df.iloc[:, 2]\n",
    "\n",
    "# 创建保存图片的文件夹\n",
    "image_dir = 'images'\n",
    "os.makedirs(image_dir, exist_ok=True)\n",
    "\n",
    "# 检查并过滤有效的JSON字符串\n",
    "def is_valid_json(json_str):\n",
    "    try:\n",
    "        json.loads(json_str)\n",
    "        return True\n",
    "    except ValueError:\n",
    "        return False\n",
    "\n",
    "valid_json_column = image_column[image_column.apply(is_valid_json)]\n",
    "\n",
    "# 下载图片的函数\n",
    "def download_image(url, file_path):\n",
    "    response = requests.get(url)\n",
    "    if response.status_code == 200:\n",
    "        with open(file_path, 'wb') as file:\n",
    "            file.write(response.content)\n",
    "        return True\n",
    "    else:\n",
    "        return False\n",
    "\n",
    "# 遍历每一行并下载图片\n",
    "for idx, row in valid_json_column.items():\n",
    "    try:\n",
    "        # 解析JSON获取图片URL\n",
    "        image_data = json.loads(row)\n",
    "        image_url = image_data['image'][0]['image']['url']\n",
    "        # 定义保存图片的路径\n",
    "        image_path = os.path.join(image_dir, f'image_{idx}.jpeg')\n",
    "        # 下载并保存图片\n",
    "        success = download_image(image_url, image_path)\n",
    "        if success:\n",
    "            print(f\"图片 {idx} 已下载并保存为 {image_path}\")\n",
    "        else:\n",
    "            print(f\"下载图片 {idx} 失败\")\n",
    "    except Exception as e:\n",
    "        print(f\"处理第 {idx} 行时出错: {e}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "转换完成！所有DOCX文件已保存到: output_docs\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import re\n",
    "from docx import Document\n",
    "import os\n",
    "\n",
    "# 读取Excel文件\n",
    "file_path = '1.xlsx'\n",
    "df = pd.read_excel(file_path)\n",
    "\n",
    "# 定义一个函数来只保留中文字符\n",
    "def keep_chinese(text):\n",
    "    return ''.join(re.findall(r'[\\u4e00-\\u9fff]', str(text)))\n",
    "\n",
    "# 清理第一列的特殊字符，只保留中文\n",
    "df.iloc[:, 0] = df.iloc[:, 0].apply(keep_chinese)\n",
    "\n",
    "# 按第一列的名字进行分组\n",
    "grouped = df.groupby(df.columns[0])\n",
    "\n",
    "# 创建输出文件夹\n",
    "output_folder = 'output_docs'\n",
    "os.makedirs(output_folder, exist_ok=True)\n",
    "\n",
    "# 遍历每个分组并保存为单独的DOCX文件\n",
    "for name, group in grouped:\n",
    "    doc = Document()\n",
    "    for index, row in group.iterrows():\n",
    "        second_col = str(row[1])\n",
    "        third_col = str(row[2])\n",
    "        doc.add_paragraph(second_col + \" \" + third_col)\n",
    "    \n",
    "    output_file_path = os.path.join(output_folder, f\"{name}.docx\")\n",
    "    doc.save(output_file_path)\n",
    "\n",
    "print(\"转换完成！所有DOCX文件已保存到:\", output_folder)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "转换完成！所有DOCX文件的字体已转换为宋体。\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "from docx import Document\n",
    "from docx.oxml.ns import qn\n",
    "from docx.shared import Pt\n",
    "\n",
    "# 指定DOCX文件所在的文件夹\n",
    "folder_path = 'output_docs'\n",
    "\n",
    "# 定义一个函数，将DOCX文件的字体转换为宋体\n",
    "def convert_font_to_songti(doc):\n",
    "    for paragraph in doc.paragraphs:\n",
    "        for run in paragraph.runs:\n",
    "            run.font.name = '宋体'\n",
    "            run._element.rPr.rFonts.set(qn('w:eastAsia'), '宋体')\n",
    "\n",
    "    for table in doc.tables:\n",
    "        for row in table.rows:\n",
    "            for cell in row.cells:\n",
    "                for paragraph in cell.paragraphs:\n",
    "                    for run in paragraph.runs:\n",
    "                        run.font.name = '宋体'\n",
    "                        run._element.rPr.rFonts.set(qn('w:eastAsia'), '宋体')\n",
    "\n",
    "# 遍历文件夹中的所有DOCX文件并进行转换\n",
    "for filename in os.listdir(folder_path):\n",
    "    if filename.endswith('.docx'):\n",
    "        file_path = os.path.join(folder_path, filename)\n",
    "        doc = Document(file_path)\n",
    "        convert_font_to_songti(doc)\n",
    "        doc.save(file_path)\n",
    "\n",
    "print(\"转换完成！所有DOCX文件的字体已转换为宋体。\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Failed to process image_0.docx: xmlns:ns0: 'xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\"' is not a valid URI, line 2, column 2804 (<string>, line 2)\n",
      "Failed to process image_104.docx: [WinError 3] 系统找不到指定的路径。: 'doc\\\\image_104.docx' -> 'doc\\\\周可/孙珉 戏剧文化与剧场实验 选修课评价.docx'\n",
      "Failed to process image_121.docx: xmlns:ns0: 'xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\"' is not a valid URI, line 2, column 2832 (<string>, line 2)\n",
      "Failed to process image_184.docx: xmlns:ns0: 'xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\"' is not a valid URI, line 2, column 2832 (<string>, line 2)\n",
      "Renamed 不详 工程伦理学 选修课评价.docx to 不详 工程伦理学 选修课评价.docx\n",
      "Renamed 不详 电子商务概论 选修课评价.docx to 不详 电子商务概论 选修课评价.docx\n",
      "Renamed 不详 走进微电子 选修课评价.docx to 不详 走进微电子 选修课评价.docx\n",
      "Renamed 东哥 岭南文化 选修课评价.docx to 东哥 岭南文化 选修课评价.docx\n",
      "Renamed 刘博 生涯规划与求职技巧 选修课评价.docx to 刘博 生涯规划与求职技巧 选修课评价.docx\n",
      "Renamed 刘喜琴 英语文学阅读与鉴赏 选修课评价.docx to 刘喜琴 英语文学阅读与鉴赏 选修课评价.docx\n",
      "Renamed 刘妍君 生涯规划与求职技巧 选修课评价.docx to 刘妍君 生涯规划与求职技巧 选修课评价.docx\n",
      "Renamed 刘畅 影视音乐赏析 选修课评价.docx to 刘畅 影视音乐赏析 选修课评价.docx\n",
      "Renamed 刘银娣 新媒体文化 选修课评价.docx to 刘银娣 新媒体文化 选修课评价.docx\n",
      "Renamed 叶代勇 智慧生活智慧化工 选修课评价.docx to 叶代勇 智慧生活智慧化工 选修课评价.docx\n",
      "Renamed 叶君，熊健 食品营养与安全 选修课评价.docx to 叶君，熊健 食品营养与安全 选修课评价.docx\n",
      "Renamed 叶圣涛 易学史 选修课评价.docx to 叶圣涛 易学史 选修课评价.docx\n",
      "Renamed 吴耀华 生涯规划与求职技巧 选修课评价.docx to 吴耀华 生涯规划与求职技巧 选修课评价.docx\n",
      "Renamed 周可 城市文化与美学 选修课评价.docx to 周可 城市文化与美学 选修课评价.docx\n",
      "Renamed 周建新 英语诗歌选读 选修课评价.docx to 周建新 英语诗歌选读 选修课评价.docx\n",
      "Renamed 周皓 法语 选修课评价.docx to 周皓 法语 选修课评价.docx\n",
      "Renamed 周育红 创业精神与创业故事会 选修课评价.docx to 周育红 创业精神与创业故事会 选修课评价.docx\n",
      "Renamed 夏正林 逻辑与思维 选修课评价.docx to 夏正林 逻辑与思维 选修课评价.docx\n",
      "Renamed 夏玲玲 生涯规划与求职技巧 选修课评价.docx to 夏玲玲 生涯规划与求职技巧 选修课评价.docx\n",
      "Renamed 屈薇 英语传媒与文化 选修课评价.docx to 屈薇 英语传媒与文化 选修课评价.docx\n",
      "Renamed 巫喜丽 新闻英语 选修课评价.docx to 巫喜丽 新闻英语 选修课评价.docx\n",
      "Renamed 张咏莲 创业法律实务 选修课评价.docx to 张咏莲 创业法律实务 选修课评价.docx\n",
      "Renamed 张小梅 葡萄酒文化入门 选修课评价.docx to 张小梅 葡萄酒文化入门 选修课评价.docx\n",
      "Renamed 很多老师 走近医学 选修课评价.docx to 很多老师 走近医学 选修课评价.docx\n",
      "Renamed 徐鹰 商务英语 选修课评价.docx to 徐鹰 商务英语 选修课评价.docx\n",
      "Renamed 戴ㄧ菲 生涯规划与求职技巧 选修课评价.docx to 戴ㄧ菲 生涯规划与求职技巧 选修课评价.docx\n",
      "Renamed 戴敏 德语初级 选修课评价.docx to 戴敏 德语初级 选修课评价.docx\n",
      "Renamed 方绘画 二十世纪美学视野中的西 选修课评价.docx to 方绘画 二十世纪美学视野中的西 选修课评价.docx\n",
      "Renamed 明宗峰 新媒体文化 选修课评价.docx to 明宗峰 新媒体文化 选修课评价.docx\n",
      "Renamed 曲志文 俄语初级 选修课评价.docx to 曲志文 俄语初级 选修课评价.docx\n",
      "Renamed 曹智频 中国传统人生智慧 选修课评价.docx to 曹智频 中国传统人生智慧 选修课评价.docx\n",
      "Renamed 朱泳媚 生涯规划与求职技巧 选修课评价.docx to 朱泳媚 生涯规划与求职技巧 选修课评价.docx\n",
      "Renamed 李博 日语初级 选修课评价.docx to 李博 日语初级 选修课评价.docx\n",
      "Renamed 李怡 中国情感文化 选修课评价.docx to 李怡 中国情感文化 选修课评价.docx\n",
      "Renamed 李沐纯 财务分析与企业发展 选修课评价.docx to 李沐纯 财务分析与企业发展 选修课评价.docx\n",
      "Renamed 李洁芳 项目管理 选修课评价.docx to 李洁芳 项目管理 选修课评价.docx\n",
      "Renamed 李牧南 产品创新设计理论与实践 选修课评价.docx to 李牧南 产品创新设计理论与实践 选修课评价.docx\n",
      "Renamed 李贺楼 公共危机管理 选修课评价.docx to 李贺楼 公共危机管理 选修课评价.docx\n",
      "Renamed 李龙一 创新管理 选修课评价.docx to 李龙一 创新管理 选修课评价.docx\n",
      "Renamed 杨俊 学术英语写作 选修课评价.docx to 杨俊 学术英语写作 选修课评价.docx\n",
      "Renamed 杨清洪 网络游戏信息检索 选修课评价.docx to 杨清洪 网络游戏信息检索 选修课评价.docx\n",
      "Renamed 林建 信息检索与利用 选修课评价.docx to 林建 信息检索与利用 选修课评价.docx\n",
      "Renamed 林影 酒文化 选修课评价.docx to 林影 酒文化 选修课评价.docx\n",
      "Renamed 柴艺娜 供应链管理 选修课评价.docx to 柴艺娜 供应链管理 选修课评价.docx\n",
      "Renamed 毕斗斗 休闲与人生 选修课评价.docx to 毕斗斗 休闲与人生 选修课评价.docx\n",
      "Renamed 沈丹 西方音乐作品欣赏 选修课评价.docx to 沈丹 西方音乐作品欣赏 选修课评价.docx\n",
      "Renamed 沈云芳 粤剧艺术赏析 选修课评价.docx to 沈云芳 粤剧艺术赏析 选修课评价.docx\n",
      "Renamed 温玲 音乐剧欣赏 选修课评价.docx to 温玲 音乐剧欣赏 选修课评价.docx\n",
      "Renamed 潘宜 音乐剧欣赏 选修课评价.docx to 潘宜 音乐剧欣赏 选修课评价.docx\n",
      "Renamed 王庆涛 信息检索与应用 选修课评价.docx to 王庆涛 信息检索与应用 选修课评价.docx\n",
      "Renamed 王怀真 生涯规划与求职技巧 选修课评价.docx to 王怀真 生涯规划与求职技巧 选修课评价.docx\n",
      "Renamed 王文梅 中国文化概论 选修课评价.docx to 王文梅 中国文化概论 选修课评价.docx\n",
      "Renamed 王玉龙 中国情感文化学 选修课评价.docx to 王玉龙 中国情感文化学 选修课评价.docx\n",
      "Renamed 王荣 生涯规划与求职技巧 选修课评价.docx to 王荣 生涯规划与求职技巧 选修课评价.docx\n",
      "Renamed 王雁 东方美术作品欣赏 选修课评价.docx to 王雁 东方美术作品欣赏 选修课评价.docx\n",
      "Renamed 程思慕 生涯规划与求职技巧 选修课评价.docx to 程思慕 生涯规划与求职技巧 选修课评价.docx\n",
      "Renamed 童真孙尉翔严玉蓉 高分子材料与工程 选修课评价.docx to 童真孙尉翔严玉蓉 高分子材料与工程 选修课评价.docx\n",
      "Renamed 统美食 舌尖上的中国微生物与传 选修课评价.docx to 统美食 舌尖上的中国微生物与传 选修课评价.docx\n",
      "Renamed 翁俊鑫 生涯规划与求职技巧 选修课评价.docx to 翁俊鑫 生涯规划与求职技巧 选修课评价.docx\n",
      "Renamed 胡兵 社会科学中的数学 选修课评价.docx to 胡兵 社会科学中的数学 选修课评价.docx\n",
      "Renamed 胡红梅 运动、营养与慢性病 选修课评价.docx to 胡红梅 运动、营养与慢性病 选修课评价.docx\n",
      "Renamed 胡青春莫海军鲁忠臣 工程认知 选修课评价.docx to 胡青春莫海军鲁忠臣 工程认知 选修课评价.docx\n",
      "Renamed 袁忠 旅游文化学 选修课评价.docx to 袁忠 旅游文化学 选修课评价.docx\n",
      "Renamed 袁锐隆 单片机开发基础 选修课评价.docx to 袁锐隆 单片机开发基础 选修课评价.docx\n",
      "Renamed 贾建忠 创业学 选修课评价.docx to 贾建忠 创业学 选修课评价.docx\n",
      "Renamed 贾建忠 营销学原理 选修课评价.docx to 贾建忠 营销学原理 选修课评价.docx\n",
      "Renamed 赵翔 新领域营销 选修课评价.docx to 赵翔 新领域营销 选修课评价.docx\n",
      "Renamed 邓晨霞 舞蹈美学和经典作品鉴赏 选修课评价.docx to 邓晨霞 舞蹈美学和经典作品鉴赏 选修课评价.docx\n",
      "Renamed 邓晶 生涯规划与求职技巧 选修课评价.docx to 邓晶 生涯规划与求职技巧 选修课评价.docx\n",
      "Renamed 邹敏 生涯规划与求职技巧 选修课评价.docx to 邹敏 生涯规划与求职技巧 选修课评价.docx\n",
      "Renamed 郭新波 运动营养与慢性病 选修课评价.docx to 郭新波 运动营养与慢性病 选修课评价.docx\n",
      "Renamed 金华 韩语会话入门 选修课评价.docx to 金华 韩语会话入门 选修课评价.docx\n",
      "Renamed 钟祎洵 东西方舞蹈文化 选修课评价.docx to 钟祎洵 东西方舞蹈文化 选修课评价.docx\n",
      "Renamed 阮熙春 庄子寓言及其智慧 选修课评价.docx to 阮熙春 庄子寓言及其智慧 选修课评价.docx\n",
      "Renamed 陆欣 英语的原理与思维 选修课评价.docx to 陆欣 英语的原理与思维 选修课评价.docx\n",
      "Renamed 陈浩文 生涯规划与求职技巧 选修课评价.docx to 陈浩文 生涯规划与求职技巧 选修课评价.docx\n",
      "Renamed 陈艳艳 英美音乐与文化 选修课评价.docx to 陈艳艳 英美音乐与文化 选修课评价.docx\n",
      "Renamed 韩双燕 生物科学与工程 选修课评价.docx to 韩双燕 生物科学与工程 选修课评价.docx\n",
      "Renamed 韩金龙 跨文化交际 选修课评价.docx to 韩金龙 跨文化交际 选修课评价.docx\n",
      "Renamed 高兵 生物科学与工程前沿 选修课评价.docx to 高兵 生物科学与工程前沿 选修课评价.docx\n",
      "Renamed 魏开 自由的观念 选修课评价.docx to 魏开 自由的观念 选修课评价.docx\n",
      "Renamed 黄汉林 西方人文经典导读 选修课评价.docx to 黄汉林 西方人文经典导读 选修课评价.docx\n",
      "Renamed 黄维文 理财学 选修课评价.docx to 黄维文 理财学 选修课评价.docx\n",
      "Renamed 黎子辉 信息检索与利用 选修课评价.docx to 黎子辉 信息检索与利用 选修课评价.docx\n",
      "Renamed 黎文 产品与品牌管理基础 选修课评价.docx to 黎文 产品与品牌管理基础 选修课评价.docx\n",
      "Renamed 齐莉莉 公益传播 选修课评价.docx to 齐莉莉 公益传播 选修课评价.docx\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "from docx import Document\n",
    "\n",
    "def extract_lines(doc):\n",
    "    lines = []\n",
    "    for para in doc.paragraphs:\n",
    "        lines.extend(para.text.split('\\n'))\n",
    "    return lines\n",
    "\n",
    "def rename_docx_files(folder_path):\n",
    "    for filename in os.listdir(folder_path):\n",
    "        if filename.endswith(\".docx\"):\n",
    "            file_path = os.path.join(folder_path, filename)\n",
    "            try:\n",
    "                doc = Document(file_path)\n",
    "                lines = extract_lines(doc)\n",
    "                if len(lines) >= 2:\n",
    "                    classname = lines[0].strip()\n",
    "                    teachername = lines[1].strip()\n",
    "                    new_filename = f\"{teachername} {classname} 选修课评价.docx\"\n",
    "                    new_file_path = os.path.join(folder_path, new_filename)\n",
    "\n",
    "                    os.rename(file_path, new_file_path)\n",
    "                    print(f\"Renamed {filename} to {new_filename}\")\n",
    "                else:\n",
    "                    print(f\"File {filename} does not have enough lines.\")\n",
    "            except Exception as e:\n",
    "                print(f\"Failed to process {filename}: {e}\")\n",
    "\n",
    "folder_path = \"doc\"  # 替换为你的文件夹路径\n",
    "rename_docx_files(folder_path)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Successfully opened: doc\\东哥_岭南文化_选修课评价.docx\n",
      "Successfully opened: doc\\交通.docx\n",
      "Successfully opened: doc\\代理咨询.docx\n",
      "Successfully opened: doc\\刘博_生涯规划与求职技巧_选修课评价.docx\n",
      "Successfully opened: doc\\刘喜琴_英语文学阅读与鉴赏_选修课评价.docx\n",
      "Successfully opened: doc\\刘妍君_生涯规划与求职技巧_选修课评价.docx\n",
      "Successfully opened: doc\\刘畅_影视音乐赏析_选修课评价.docx\n",
      "Successfully opened: doc\\刘银娣_新媒体文化_选修课评价.docx\n",
      "Successfully opened: doc\\医院.docx\n",
      "Successfully opened: doc\\叶代勇_智慧生活智慧化工_选修课评价.docx\n",
      "Successfully opened: doc\\叶君，熊健_食品营养与安全_选修课评价.docx\n",
      "Successfully opened: doc\\叶圣涛_易学史_选修课评价.docx\n",
      "Successfully opened: doc\\吴耀华_生涯规划与求职技巧_选修课评价.docx\n",
      "Successfully opened: doc\\周可_城市文化与美学_选修课评价.docx\n",
      "Successfully opened: doc\\周建新_英语诗歌选读_选修课评价.docx\n",
      "Successfully opened: doc\\周皓_法语_选修课评价.docx\n",
      "Successfully opened: doc\\周育红_创业精神与创业故事会_选修课评价.docx\n",
      "Successfully opened: doc\\夏正林_逻辑与思维_选修课评价.docx\n",
      "Successfully opened: doc\\夏玲玲_生涯规划与求职技巧_选修课评价.docx\n",
      "Successfully opened: doc\\学院介绍.docx\n",
      "Successfully opened: doc\\屈薇_英语传媒与文化_选修课评价.docx\n",
      "Successfully opened: doc\\工程伦理学_选修课评价.docx\n",
      "Successfully opened: doc\\巫喜丽_新闻英语_选修课评价.docx\n",
      "Successfully opened: doc\\张咏莲_创业法律实务_选修课评价.docx\n",
      "Successfully opened: doc\\张小梅_葡萄酒文化入门_选修课评价.docx\n",
      "Successfully opened: doc\\很多老师_走近医学_选修课评价.docx\n",
      "Successfully opened: doc\\徐鹰_商务英语_选修课评价.docx\n",
      "Successfully opened: doc\\戏剧文化与剧场实验_选修课评价.docx\n",
      "Successfully opened: doc\\戴ㄧ菲_生涯规划与求职技巧_选修课评价.docx\n",
      "Successfully opened: doc\\戴敏_德语初级_选修课评价.docx\n",
      "Successfully opened: doc\\方绘画_二十世纪美学视野中的西_选修课评价.docx\n",
      "Successfully opened: doc\\明宗峰_新媒体文化_选修课评价.docx\n",
      "Successfully opened: doc\\曲志文_俄语初级_选修课评价.docx\n",
      "Successfully opened: doc\\曹智频_中国传统人生智慧_选修课评价.docx\n",
      "Successfully opened: doc\\朱泳媚_生涯规划与求职技巧_选修课评价.docx\n",
      "Successfully opened: doc\\李博_日语初级_选修课评价.docx\n",
      "Successfully opened: doc\\李怡_中国情感文化_选修课评价.docx\n",
      "Successfully opened: doc\\李沐纯_财务分析与企业发展_选修课评价.docx\n",
      "Successfully opened: doc\\李洁芳_项目管理_选修课评价.docx\n",
      "Successfully opened: doc\\李牧南_产品创新设计理论与实践_选修课评价.docx\n",
      "Successfully opened: doc\\李贺楼_公共危机管理_选修课评价.docx\n",
      "Successfully opened: doc\\李龙一_创新管理_选修课评价.docx\n",
      "Successfully opened: doc\\杨俊_学术英语写作_选修课评价.docx\n",
      "Successfully opened: doc\\杨清洪_网络游戏信息检索_选修课评价.docx\n",
      "Successfully opened: doc\\林建_信息检索与利用_选修课评价.docx\n",
      "Successfully opened: doc\\林影_酒文化_选修课评价.docx\n",
      "Successfully opened: doc\\柴艺娜_供应链管理_选修课评价.docx\n",
      "Successfully opened: doc\\毕斗斗_休闲与人生_选修课评价.docx\n",
      "Successfully opened: doc\\沈丹_西方音乐作品欣赏_选修课评价.docx\n",
      "Successfully opened: doc\\沈云芳_粤剧艺术赏析_选修课评价.docx\n",
      "Successfully opened: doc\\温玲_音乐剧欣赏_选修课评价.docx\n",
      "Successfully opened: doc\\潘宜_音乐剧欣赏_选修课评价.docx\n",
      "Successfully opened: doc\\王庆涛_信息检索与应用_选修课评价.docx\n",
      "Successfully opened: doc\\王怀真_生涯规划与求职技巧_选修课评价.docx\n",
      "Successfully opened: doc\\王文梅_中国文化概论_选修课评价.docx\n",
      "Successfully opened: doc\\王玉龙_中国情感文化学_选修课评价.docx\n",
      "Successfully opened: doc\\王荣_生涯规划与求职技巧_选修课评价.docx\n",
      "Successfully opened: doc\\王雁_东方美术作品欣赏_选修课评价.docx\n",
      "Successfully opened: doc\\生活.docx\n",
      "Successfully opened: doc\\电子商务概论_选修课评价.docx\n",
      "Successfully opened: doc\\程思慕_生涯规划与求职技巧_选修课评价.docx\n",
      "Successfully opened: doc\\童真孙尉翔严玉蓉_高分子材料与工程_选修课评价.docx\n",
      "Successfully opened: doc\\统美食_舌尖上的中国微生物与传_选修课评价.docx\n",
      "Successfully opened: doc\\维修.docx\n",
      "Successfully opened: doc\\综测.docx\n",
      "Successfully opened: doc\\网络.docx\n",
      "Successfully opened: doc\\翁俊鑫_生涯规划与求职技巧_选修课评价.docx\n",
      "Successfully opened: doc\\胡兵_社会科学中的数学_选修课评价.docx\n",
      "Successfully opened: doc\\胡红梅_运动、营养与慢性病_选修课评价.docx\n",
      "Successfully opened: doc\\胡青春莫海军鲁忠臣_工程认知_选修课评价.docx\n",
      "Successfully opened: doc\\薛锋_材料与社会_选修课评价.docx\n",
      "Successfully opened: doc\\袁忠_旅游文化学_选修课评价.docx\n",
      "Successfully opened: doc\\袁锐隆_单片机开发基础_选修课评价.docx\n",
      "Successfully opened: doc\\课程.docx\n",
      "Successfully opened: doc\\账号密码.docx\n",
      "Successfully opened: doc\\购物.docx\n",
      "Successfully opened: doc\\贾建忠_创业学_选修课评价.docx\n",
      "Successfully opened: doc\\贾建忠_营销学原理_选修课评价.docx\n",
      "Successfully opened: doc\\贾海平_日语初级_选修课评价.docx\n",
      "Successfully opened: doc\\走进微电子_选修课评价.docx\n",
      "Successfully opened: doc\\赵翔_新领域营销_选修课评价.docx\n",
      "Successfully opened: doc\\邓晨霞_舞蹈美学和经典作品鉴赏_选修课评价.docx\n",
      "Successfully opened: doc\\邓晶_生涯规划与求职技巧_选修课评价.docx\n",
      "Successfully opened: doc\\邹敏_生涯规划与求职技巧_选修课评价.docx\n",
      "Successfully opened: doc\\郑学丹_英汉语法比较拾趣_选修课评价.docx\n",
      "Successfully opened: doc\\郭新波_运动营养与慢性病_选修课评价.docx\n",
      "Successfully opened: doc\\金华_韩语会话入门_选修课评价.docx\n",
      "Successfully opened: doc\\钟祎洵_东西方舞蹈文化_选修课评价.docx\n",
      "Successfully opened: doc\\银行.docx\n",
      "Successfully opened: doc\\阮熙春_庄子寓言及其智慧_选修课评价.docx\n",
      "Successfully opened: doc\\陆欣_英语的原理与思维_选修课评价.docx\n",
      "Successfully opened: doc\\陈浩文_生涯规划与求职技巧_选修课评价.docx\n",
      "Successfully opened: doc\\陈艳艳_英美音乐与文化_选修课评价.docx\n",
      "Successfully opened: doc\\韩双燕_生物科学与工程_选修课评价.docx\n",
      "Successfully opened: doc\\韩金龙_跨文化交际_选修课评价.docx\n",
      "Successfully opened: doc\\餐饮.docx\n",
      "Successfully opened: doc\\高兵_生物科学与工程前沿_选修课评价.docx\n",
      "Successfully opened: doc\\魏开_自由的观念_选修课评价.docx\n",
      "Successfully opened: doc\\黄汉林_西方人文经典导读_选修课评价.docx\n",
      "Successfully opened: doc\\黄维文_理财学_选修课评价.docx\n",
      "Successfully opened: doc\\黎子辉_信息检索与利用_选修课评价.docx\n",
      "Successfully opened: doc\\黎文_产品与品牌管理基础_选修课评价.docx\n",
      "Successfully opened: doc\\齐莉莉_公益传播_选修课评价.docx\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "from docx import Document\n",
    "\n",
    "def check_docx_files(directory):\n",
    "    for filename in os.listdir(directory):\n",
    "        if filename.endswith('.docx'):\n",
    "            file_path = os.path.join(directory, filename)\n",
    "            try:\n",
    "                doc = Document(file_path)\n",
    "                # If no exception is raised, the file is considered valid\n",
    "                print(f'Successfully opened: {file_path}')\n",
    "            except Exception as e:\n",
    "                # Print the error message for files that raise an exception\n",
    "                print(f'Error reading {file_path}: {e}')\n",
    "\n",
    "# Specify the directory to check\n",
    "directory = 'doc'\n",
    "\n",
    "# Check all .docx files in the specified directory\n",
    "check_docx_files(directory)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Files with empty \"knowledges\" have been copied to the target directory:\n",
      "代理咨询.json\n",
      "医院.json\n",
      "叶圣涛_易学史_选修课评价.json\n",
      "学院介绍.json\n",
      "方绘画_二十世纪美学视野中的西_选修课评价.json\n",
      "李牧南_产品创新设计理论与实践_选修课评价.json\n",
      "李龙一_创新管理_选修课评价.json\n",
      "沈云芳_粤剧艺术赏析_选修课评价.json\n",
      "维修.json\n",
      "账号密码.json\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import json\n",
    "import shutil\n",
    "\n",
    "def check_and_move_empty_knowledges(directory, target_directory):\n",
    "    # Create the target directory if it doesn't exist\n",
    "    if not os.path.exists(target_directory):\n",
    "        os.makedirs(target_directory)\n",
    "    \n",
    "    empty_knowledges_files = []\n",
    "    \n",
    "    for filename in os.listdir(directory):\n",
    "        if filename.endswith('.json'):\n",
    "            file_path = os.path.join(directory, filename)\n",
    "            try:\n",
    "                with open(file_path, 'r', encoding='utf-8') as file:\n",
    "                    data = json.load(file)\n",
    "                    if 'knowledges' in data and not data['knowledges']:\n",
    "                        empty_knowledges_files.append(filename)\n",
    "                        # Copy the file to the target directory\n",
    "                        shutil.copy(file_path, os.path.join(target_directory, filename))\n",
    "            except json.JSONDecodeError as e:\n",
    "                print(f'Error reading {file_path}: {e}')\n",
    "    \n",
    "    return empty_knowledges_files\n",
    "\n",
    "# Specify the directory containing the JSON files\n",
    "source_directory = 'json'\n",
    "# Specify the target directory to copy files with empty \"knowledges\"\n",
    "target_directory = 'json1'\n",
    "\n",
    "# Check and move the files with empty \"knowledges\"\n",
    "empty_files = check_and_move_empty_knowledges(source_directory, target_directory)\n",
    "\n",
    "# Print the list of files\n",
    "print('Files with empty \"knowledges\" have been copied to the target directory:')\n",
    "for filename in empty_files:\n",
    "    print(filename)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
